#include "PlayerManager.h"
#include "Utils.h"
#include <Windows.h>
#include "RedisManager.h"

using namespace utgame;


PlayerManager::PlayerManager()
{

}

bool PlayerManager::IsPlayerExist(const std::string &strName)
{
	return true;
}

int PlayerManager::GetAllPlayer(std::vector<std::string> &vecPlayerName)
{
	std::map<std::string, std::string> mapData;
	RedisManager::getInstance().HashGetAll(RedisManager::DB_PLAYER, mapData);

	for (auto ite = mapData.cbegin(); ite != mapData.cend(); ++ite)
	{
		const std::string &strValue = ite->second;
		PlayerInfo playerInfo;
		playerInfo.ParseFromString(strValue);

		vecPlayerName.push_back(playerInfo.name());
	}

	return 0;
}

int PlayerManager::GetPlayer(const std::string &strName, PlayerInfo &playerInfo)
{
	std::map<std::string, std::string> mapData;
	RedisManager::getInstance().HashGetAll(RedisManager::DB_PLAYER, mapData);

	for (auto ite = mapData.cbegin(); ite != mapData.cend(); ++ite)
	{
		const std::string &strValue = ite->second;
		PlayerInfo info;
		info.ParseFromString(strValue);

		if (info.name() == strName)
		{
			playerInfo.CopyFrom(info);
			return 0;
		}
	}
	return -1;
}

std::string PlayerManager::AddPlayer(const std::string &strName, const std::string &strPwd)
{
	std::string strId = Utils::GenerateUUID();
	PlayerInfo playerInfo;
	playerInfo.set_id(strId);
	playerInfo.set_name(strName);
	playerInfo.set_pwd(strPwd);
	playerInfo.set_create_time(GetTickCount());

	std::string strPlayerInfo;
	playerInfo.SerializeToString(&strPlayerInfo);

	RedisManager::getInstance().HashSet(RedisManager::DB_PLAYER, strId, strPlayerInfo);

	return strId;
}

